layui.define(['table', 'form'], function(exports){
	var $ = layui.$,table = layui.table,form = layui.form;

	table.render({
	    elem: '#LAY-table'
	    ,url: layui.setter.webRoot + '/market/project/order/list.data'
	    ,method: 'post'
	    ,parseData: function (res) {
	        return {
	            "code": res.code,
	            "msg": res.msg,
	            "count": res.body.total,
	            "data": res.body.data
	        };
	    }
	    ,response: {statusCode: 200}
	    ,cols: [[
	      {field: 'tradeNo', title: '订单号'}
			,{field: 'businessTypeName', title: '业务类型'}
			,{field: 'goodsCode', title: '商品来源', templet:function(record){
				if(record.goodsCode){
					return '云商品';
				}else{
					return '自有商品';
				}
			}}
	      ,{field: 'goodsName', title: '商品名称'}
	      ,{field: 'goodsCount', title: '商品数量'}
	      ,{field: 'member.nickname', title: '会员昵称', templet:function(record){
				if(record.memberNickname){
					return record.memberNickname;
				}else{
					return record.memberId;
				}
			}}
	      ,{field: 'payTypeName', title: '支付类型'}
	      ,{field: 'moneyStr', title: '订单金额'}
		  ,{field: 'supplyStateName', width: 100, title: '供货状态'}
	      ,{field: 'stateName', width: 100, title: '状态', templet:function(record){
					if(record.state == 2){
						return '<span class="layui-badge layui-bg-green">'+record.stateName+'</span>';
					}else if(record.state == 3 || record.state == 5 ){
						return '<span class="layui-badge layui-bg-blue">'+record.stateName+'</span>';
					}else if(record.state == 4){
						return '<span class="layui-badge layui-bg-orange">'+record.stateName+'</span>';
					}else {
						return record.stateName;
					}
				}}
	      ,{field: 'createDate', title: '创建时间'}
	      ,{title: '操作', width: 200, align:'center', fixed: 'right', templet: function (record) {
			  	var html = '<a class="layui-btn layui-btn-xs" lay-event="operate">操作</a>';
				if(record.goodsCode && record.supplyState > 1){
					html = html + '<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="cloudInfo">云订单</a>';
				}
				html = html + '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>';
			  	return html;
			}}
	    ]]
	    ,page: true
	    ,limit: 30
	    ,height: 'full-220'
	    ,text: '对不起，加载出现异常！'
  	});

	//监听搜索
	form.on('submit(LAY-search)', function(data){
		var field = data.field;
		//执行重载
		table.reload('LAY-table', {
			where: field
		});
	});

	var active = {
		operate:function(obj) {
			layer.open({
				type: 2
				, title: '操作订单'
				, content: layui.setter.webRoot + '/market/project/order/operate.html?tradeNo=' + obj.data.tradeNo
				, maxmin: true
				, area: ['90%', '90%']
				, btn: ['取消']
				,end :function (){
					table.reload('LAY-table');
				}
			});
		}
		,del:function(obj){
	    	layer.confirm('确定要删除该商品购买订单吗？', function(index){
		    	$.ajax({
		      	  type: "post",
		          url: layui.setter.webRoot + '/market/project/order/del.do',
		          data:{tradeNo:obj.data.tradeNo},
		  		  dataType:"json",
		  		  success:function(res){
		  			if(res.code == 200){
		  				obj.del();
		  			}else{
		  				layer.msg(res.msg,{icon: 5});
		  			}
		  		  }
		        });
		    	layer.close(index);
		    });
	    }
	}

	table.on('tool(LAY-table)', function(obj){
		var type = obj.event ;
		active[type] ? active[type].call(this,obj) : '';
	});

	exports('goods_order_list', {})
});